home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Tool Chest / Games / Game Sample Code / SpriteWorld 1.0b3 / Examples / SpaceRocks / Application.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-13  |  10.6 KB  |  743 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. //    Application.c
  3. //
  4. //    Created:    Sunday, April 11, 1993
  5. //    By:        Tony Myles
  6. //
  7. //    Copyright: © 1993 Tony Myles, All rights reserved worldwide.
  8. ///--------------------------------------------------------------------------------------
  9.  
  10.  
  11. #if THINK_C
  12. #ifndef __BDC__
  13. #include <BDC.h>
  14. #endif
  15. #else
  16. #ifndef __PACKAGES__
  17. #include <Packages.h>
  18. #endif
  19. #endif
  20.  
  21. #ifndef __APPLEEVENTS__
  22. #include <AppleEvents.h>
  23. #endif
  24.  
  25. #ifndef __DESK__
  26. #include <Desk.h>
  27. #endif
  28.  
  29. #ifndef __DIALOGS__
  30. #include <Dialogs.h>
  31. #endif
  32.  
  33. #ifndef __DISKINIT__
  34. #include <DiskInit.h>
  35. #endif
  36.  
  37. #ifndef __EPPC__
  38. #include <EPPC.h>
  39. #endif
  40.  
  41. #ifndef __EVENTS__
  42. #include <Events.h>
  43. #endif
  44.  
  45. #ifndef __ERRORS__
  46. #include <Errors.h>
  47. #endif
  48.  
  49. #ifndef __FONTS__
  50. #include <Fonts.h>
  51. #endif
  52.  
  53. #ifndef __GESTALTEQU__
  54. #include <GestaltEqu.h>
  55. #endif
  56.  
  57. #ifndef __MENUS__
  58. #include <Menus.h>
  59. #endif
  60.  
  61. #ifndef __RESOURCES__
  62. #include <Resources.h>
  63. #endif
  64.  
  65. #ifndef __OSEVENTS__
  66. #include <OSEvents.h>
  67. #endif
  68.  
  69. #ifndef __TEXTEDIT__
  70. #include <TextEdit.h>
  71. #endif
  72.  
  73. #ifndef __TRAPS__
  74. #include <Traps.h>
  75. #endif
  76.  
  77. #ifndef __TOOLUTILS__
  78. #include <ToolUtils.h>
  79. #endif
  80.  
  81. #ifndef __WINDOWS__
  82. #include <Windows.h>
  83. #endif
  84.  
  85. #ifndef __SPRITEWORLD__
  86. #include <SpriteWorld.h>
  87. #endif
  88.  
  89. #ifndef __APPLICATION__
  90. #include "Application.h"
  91. #endif
  92.  
  93. #ifndef __SPACEGAME__
  94. #include "SpaceGame.h"
  95. #endif
  96.  
  97. #ifndef __DEBUGUTILS__
  98. #include "DebugUtils.h"
  99. #endif
  100.  
  101.  
  102. Boolean gIsRunning = true;
  103. Boolean gInBackGround = false;
  104. Boolean gHasWaitNextEvent = false;
  105. WindowPtr gWindowP = NULL;
  106. SpaceGamePtr gSpaceGameP = NULL;
  107.  
  108.  
  109. void main(void)
  110. {
  111.     OSErr err = noErr;
  112.  
  113.     Initialize(kNumberOfMoreMastersCalls);
  114.  
  115.     if (CheckSystem())
  116.     {
  117.         if (HasAppleEvents())
  118.         {
  119.             err = InstallAppleEventHandlers();
  120.  
  121.             if (err != noErr)
  122.             {
  123.                 ErrorAlert(err, kUnknownErrorStringIndex);
  124.             }
  125.         }
  126.  
  127.         CreateMenuBar();
  128.  
  129.         if (EnterApplication())
  130.         {
  131.             ServiceEvents();
  132.         }
  133.  
  134.         ExitApplication();
  135.     }
  136.     else
  137.     {
  138.         CantRunOnThisMachine();
  139.     }
  140.  
  141.     ExitToShell();
  142. }
  143.  
  144.  
  145. void Initialize(
  146.     short numberOfMasters)
  147. {
  148.     EventRecord tempEvent;
  149.  
  150.     MaxApplZone();
  151.  
  152.     while (numberOfMasters--)
  153.     {
  154.         MoreMasters();
  155.     }
  156.  
  157.     InitGraf(&qd.thePort);
  158.     InitFonts();
  159.     InitWindows();
  160.     InitMenus();
  161.     TEInit();
  162.     InitDialogs(NULL);
  163.     InitCursor();
  164.     FlushEvents(everyEvent, 0);
  165.  
  166.     (void)EventAvail(everyEvent, &tempEvent);
  167.     (void)EventAvail(everyEvent, &tempEvent);
  168.     (void)EventAvail(everyEvent, &tempEvent);
  169.  
  170.     gHasWaitNextEvent = HasWaitNextEvent();
  171. }
  172.  
  173.  
  174. Boolean CheckSystem(void)
  175. {
  176.     OSErr    err;
  177.     Boolean isSystemGood = true;
  178.     long    gestaltResult;
  179.  
  180.     err = Gestalt(gestaltTimeMgrVersion, &gestaltResult);
  181.  
  182.     isSystemGood = (err == noErr) && (gestaltResult >= gestaltStandardTimeMgr);
  183.  
  184.     if (!isSystemGood)
  185.     {
  186.         CantRunOnThisMachine();
  187.     }
  188.  
  189.     return isSystemGood;
  190. }
  191.  
  192.  
  193. Boolean HasAppleEvents(void)
  194. {
  195.     Boolean hasAppleEvents;
  196.     OSErr err;
  197.     long gestaltResult;
  198.  
  199.     err = Gestalt(gestaltAppleEventsAttr, &gestaltResult);
  200.  
  201.     if (err == noErr)
  202.     {
  203.         hasAppleEvents = (gestaltResult & (1 << gestaltAppleEventsPresent)) != 0;
  204.     }
  205.     else
  206.     {
  207.         hasAppleEvents = false;
  208.     }
  209.  
  210.     return hasAppleEvents;
  211. }
  212.  
  213.  
  214. OSErr InstallAppleEventHandlers(void)
  215. {
  216.     OSErr err = noErr;
  217.  
  218.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, HandleOpenApp, 0, false);
  219.  
  220.     if (err == noErr)
  221.     {
  222.         err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, HandleOpenDoc, 0, false);
  223.     }
  224.  
  225.     if (err == noErr)
  226.     {
  227.         err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, HandlePrintDoc, 0, false);
  228.     }
  229.  
  230.     if (err == noErr)
  231.     {
  232.         err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, HandleQuit, 0, false);
  233.     }
  234.  
  235.     return err;
  236. }
  237.  
  238.  
  239. void CreateMenuBar(void)
  240. {
  241.     Handle menuBarH;
  242.  
  243.     menuBarH = GetNewMBar(kMenuBarResID);
  244.  
  245.     if (menuBarH != NULL)
  246.     {
  247.         SetMenuBar(menuBarH);
  248.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  249.         DrawMenuBar();
  250.     }
  251.     else
  252.     {
  253.         CantFindResource();
  254.     }
  255. }
  256.  
  257.  
  258. void CreateWindow(void)
  259. {
  260.     gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  261.  
  262.     if (gWindowP != NULL)
  263.     {
  264.         SizeWindow(gWindowP, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom, false);
  265.         MoveWindow(gWindowP, 0, 0, false);
  266.     }
  267.     else
  268.     {
  269.         CantFindResource();
  270.     }
  271. }
  272.  
  273.  
  274. Boolean EnterApplication(void)
  275. {
  276.     OSErr    err;
  277.     short oldResRefNum, newResRefNum;
  278.     Str255 fileName;
  279.  
  280.     SetCursor(*GetCursor(watchCursor));
  281.  
  282.     GetIndString(fileName, kGameStringsResID, kGraphicsFileNameStringIndex);
  283.  
  284.     if (fileName[0] == 0)
  285.     {
  286.         CantFindResource();
  287.     }
  288.  
  289.     oldResRefNum = CurResFile();
  290.     newResRefNum = OpenResFile(fileName);
  291.     err = ResError();
  292.  
  293.     if (err == noErr)
  294.     {
  295.         UseResFile(newResRefNum);
  296.     }
  297.  
  298.     if (err == noErr)
  299.     {
  300.         err = EnterSpaceGame();
  301.     }
  302.  
  303.     if (err == noErr)
  304.     {
  305.         err = CreateSpaceGame(&gSpaceGameP);
  306.     }
  307.  
  308.     if (newResRefNum != -1)
  309.     {
  310.         UseResFile(oldResRefNum);
  311.         CloseResFile(newResRefNum);
  312.     }
  313.  
  314.     SetCursor(&qd.arrow);
  315.  
  316.     if (err != noErr)
  317.     {
  318.         ErrorAlert(err, kUnknownErrorStringIndex);
  319.     }
  320.  
  321.     return err == noErr;
  322. }
  323.  
  324.  
  325. void ExitApplication(void)
  326. {
  327.     if (gSpaceGameP != NULL)
  328.     {
  329.         DisposeSpaceGame(gSpaceGameP);
  330.         gSpaceGameP = NULL;
  331.     }
  332.  
  333.     ExitSpaceGame();
  334.  
  335.     if (gWindowP != NULL)
  336.     {
  337.         DisposeWindow(gWindowP);
  338.         gWindowP = NULL;
  339.     }
  340. }
  341.  
  342.  
  343. void ServiceEvents(void)
  344. {
  345.     Boolean haveEvent;
  346.     EventRecord event;
  347.     long sleepTime;
  348.     RgnHandle mouseRgn = gHasWaitNextEvent ? NewRgn() : NULL;
  349.  
  350.     while (gIsRunning)
  351.     {
  352.         if (gHasWaitNextEvent)
  353.         {
  354.             sleepTime = gInBackGround ? kBackGroundSleepTime : kForeGroundSleepTime;
  355.  
  356.             haveEvent = WaitNextEvent(everyEvent, &event, sleepTime, mouseRgn);
  357.         }
  358.         else
  359.         {
  360.             SystemTask();
  361.             haveEvent = GetNextEvent(everyEvent, &event);
  362.         }
  363.  
  364.         if (haveEvent)
  365.         {
  366.             DispatchEvent(&event);
  367.         }
  368.         else
  369.         {
  370.             HandleNullEvent();
  371.         }
  372.     }
  373.  
  374.     if (mouseRgn != NULL)
  375.     {
  376.         DisposeRgn(mouseRgn);
  377.     }
  378. }
  379.  
  380.  
  381. void DispatchEvent(
  382.     EventRecord* event)
  383. {
  384.     switch(event->what)
  385.     {
  386.         case mouseDown:            HandleMouseEvent(event);
  387.         break;
  388.         case mouseUp:
  389.         break;
  390.         case keyUp:
  391.         break;
  392.         case keyDown:
  393.         case autoKey:                HandleKeyEvent((char)(event->message & charCodeMask), event->modifiers);
  394.         break;
  395.         case updateEvt:            HandleUpdateEvent((WindowPtr)event->message);
  396.         break;
  397.         case diskEvt:                HandleDiskEvent(event->message);
  398.         break;
  399.         case activateEvt:            HandleActivateEvent((WindowPtr)event->message);
  400.         break;
  401.         case networkEvt:
  402.         break;
  403.         case driverEvt:
  404.         break;
  405.         case app1Evt:
  406.         break;
  407.         case app2Evt:
  408.         break;
  409.         case app3Evt:
  410.         break;
  411.         case osEvt:                    HandleOSEvent(event->message);
  412.         break;
  413.         case kHighLevelEvent:    AEProcessAppleEvent(event);
  414.         default:
  415.         break;
  416.     }
  417. }
  418.  
  419.  
  420. void HandleMouseEvent(
  421.     EventRecord* event)
  422. {
  423.     WindowPtr whichWindow;
  424.     short partCode;
  425.  
  426.     partCode = FindWindow(event->where, &whichWindow);
  427.  
  428.     switch (partCode)
  429.     {
  430.         case inDesk:        
  431.         break;
  432.         case inMenuBar:        HandleMenuCommand(MenuSelect(event->where));
  433.         break;
  434.         case inSysWindow:        SystemClick(event, whichWindow);
  435.         break;
  436.         case inContent:
  437.         break;
  438.         case inDrag:
  439.         break;
  440.         case inGrow:
  441.         break;
  442.         case inGoAway:
  443.         break;
  444.         case inZoomIn:
  445.         case inZoomOut:
  446.         break;
  447.         default:            
  448.         break;
  449.     }
  450. }
  451.  
  452.  
  453. void HandleKeyEvent(
  454.     char key,
  455.     short modifiers)
  456. {
  457.     if ((modifiers & cmdKey) != 0)
  458.     {
  459.         HandleMenuCommand(MenuKey(key));
  460.     }
  461.     else
  462.     {
  463.         // do whatever
  464.     }
  465. }
  466.  
  467.  
  468. void HandleUpdateEvent(
  469.     WindowPtr updateWindowP)
  470. {
  471.     if (updateWindowP == gWindowP)
  472.     {
  473.         SetPort(updateWindowP);
  474.         BeginUpdate(updateWindowP);
  475.  
  476.         if (gSpaceGameP != NULL)
  477.         {
  478.             SWLockSpriteWorld(gSpaceGameP->gameSpriteWorldP);
  479.             SWUpdateSpriteWorld(gSpaceGameP->gameSpriteWorldP);
  480.             SWUnlockSpriteWorld(gSpaceGameP->gameSpriteWorldP);
  481.         }
  482.  
  483.         EndUpdate(updateWindowP);
  484.     }
  485. }
  486.  
  487.  
  488. void HandleActivateEvent(
  489.     WindowPtr updateWindowP)
  490. {
  491. }
  492.  
  493.  
  494. void HandleOSEvent(
  495.     long message)
  496. {
  497.     if ((message >> 24) == suspendResumeMessage)
  498.     {
  499.         if ((message & resumeFlag) != 0)
  500.         {
  501.             gInBackGround = false;
  502.         }
  503.         else
  504.         {
  505.             gInBackGround = true;
  506.         }
  507.     }
  508. }
  509.  
  510.  
  511. void HandleDiskEvent(
  512.     long message)
  513. {
  514.     OSErr err;
  515.     Point dialogLocation = {100, 100};
  516.  
  517.     if ((message & 0xFFFF0000) != noErr)
  518.     {
  519.         err = DIBadMount(dialogLocation, message);
  520.  
  521.         if (err != noErr)
  522.         {
  523.             ErrorAlert(err, kUnknownErrorStringIndex);
  524.         }
  525.     }
  526. }
  527.  
  528.  
  529. void HandleNullEvent(void)
  530. {
  531.     if (gSpaceGameP != NULL)
  532.         IdleSpaceGame(gSpaceGameP);
  533. }
  534.  
  535.  
  536. void HandleMenuCommand(
  537.     long menuItemIdentifier)
  538. {
  539.     short menuIdent = HiWord(menuItemIdentifier);
  540.     short menuItem = LoWord(menuItemIdentifier);
  541.  
  542.     switch (menuIdent)
  543.     {
  544.         case kAppleMenuID:
  545.         {
  546.             HandleAppleMenuCommand(menuItem);
  547.             break;
  548.         }
  549.  
  550.         case kFileMenuID:
  551.         {
  552.             HandleFileMenuCommand(menuItem);
  553.             break;
  554.         }
  555.  
  556.         case kEditMenuID:
  557.         {
  558.             HandleEditMenuCommand(menuItem);
  559.             break;
  560.         }
  561.     }
  562.  
  563.     HiliteMenu(0);
  564. }
  565.  
  566.  
  567. void HandleAppleMenuCommand(
  568.     short menuItem)
  569. {
  570.     Str255 deskAccName;
  571.  
  572.     switch (menuItem)
  573.     {
  574.         case kAboutItem:
  575.         {
  576.             SysBeep(1);
  577.             break;
  578.         }
  579.  
  580.         default:
  581.         {
  582.             GetItem(GetMHandle(kAppleMenuID), menuItem, deskAccName);
  583.             OpenDeskAcc(deskAccName);
  584.             break;
  585.         }
  586.     }
  587. }
  588.  
  589.  
  590. void HandleFileMenuCommand(
  591.     short menuItem)
  592. {
  593.     switch (menuItem)
  594.     {
  595.         case kNewItem:
  596.         {
  597.             if (gSpaceGameP != NULL)
  598.                 StartSpaceGame(gSpaceGameP);
  599.             break;
  600.         }
  601.  
  602.         case kQuitItem:
  603.         {
  604.             gIsRunning = false;
  605.             break;
  606.         }
  607.     }
  608. }
  609.  
  610.  
  611. void HandleEditMenuCommand(
  612.     short menuItem)
  613. {
  614.     (void)SystemEdit(menuItem);
  615. }
  616.  
  617.  
  618. pascal OSErr HandleOpenApp(
  619.     AppleEvent srcAppleEvent,
  620.     AppleEvent replyAppleEvent,
  621.     long refCon)
  622. {
  623.     return noErr;
  624. }
  625.  
  626.  
  627. pascal OSErr HandleOpenDoc(
  628.     AppleEvent srcAppleEvent,
  629.     AppleEvent replyAppleEvent,
  630.     long refCon)
  631. {
  632.     return errAEEventNotHandled;
  633. }
  634.  
  635.  
  636. pascal OSErr HandlePrintDoc(
  637.     AppleEvent srcAppleEvent,
  638.     AppleEvent replyAppleEvent,
  639.     long refCon)
  640. {
  641.     return errAEEventNotHandled;
  642. }
  643.  
  644.  
  645. pascal OSErr HandleQuit(
  646.     AppleEvent srcAppleEvent,
  647.     AppleEvent replyAppleEvent,
  648.     long refCon)
  649. {
  650.     gIsRunning = false;
  651.  
  652.     return noErr;
  653. }
  654.  
  655.  
  656. void ErrorAlert(
  657.     OSErr err,
  658.     short errorStringIndex)
  659. {
  660.     Str255 messageString, errorString;
  661.  
  662.     GetIndString(messageString, kErrorStringListResID, errorStringIndex);
  663.  
  664.     if (messageString[0] == 0)
  665.     {
  666.         BlockMove(kSeriousDamageString, messageString, sizeof(kSeriousDamageString));
  667.     }
  668.  
  669.     NumToString(err, errorString);
  670.  
  671.     ParamText(messageString, errorString, "\p", "\p");
  672.  
  673.     (void)StopAlert(kErrorAlertResID, NULL);
  674. }
  675.  
  676.  
  677. void CantFindResource(void)
  678. {
  679.     OSErr err;
  680.  
  681.     err = ResError();
  682.  
  683.     if (err == noErr)
  684.     {
  685.         err = resNotFound;
  686.     }
  687.  
  688.     ErrorAlert(err, kCantFindResourceStringIndex);
  689.  
  690.     ExitToShell();
  691. }
  692.  
  693.  
  694. void CantRunOnThisMachine(void)
  695. {
  696.     (void)StopAlert(kCantRunAlertResID, NULL);
  697. }
  698.  
  699.  
  700. short NumToolboxTraps(void)
  701. {
  702.     return    (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  703.                 ?    0x0200 :    0x0400;
  704. }
  705.  
  706.  
  707. TrapType GetTrapType(
  708.     short trap)
  709. {
  710.     #define TrapMask 0x0800
  711.  
  712.     return ((trap & TrapMask) != 0) ? ToolTrap : OSTrap;
  713. }
  714.  
  715.  
  716. Boolean TrapAvail(
  717.     short trap)
  718. {
  719.     TrapType    tType;
  720.  
  721.     tType = GetTrapType(trap);
  722.     if (tType == ToolTrap)
  723.     {
  724.         trap = trap & 0x07FF;
  725.     }
  726.  
  727.     if (trap >= NumToolboxTraps())
  728.     {
  729.         trap = _Unimplemented;
  730.     }
  731.  
  732.     return NGetTrapAddress(trap, tType) !=
  733.              NGetTrapAddress(_Unimplemented, ToolTrap);
  734. }
  735.  
  736.  
  737. Boolean HasWaitNextEvent(void)
  738. {
  739.     return TrapAvail(_WaitNextEvent);
  740. }
  741.  
  742.  
  743.